package phoneBook;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;
import java.util.StringTokenizer;

import arcademis.Marshalable;

public class PhoneBook extends rme.server.RmeRemoteObject implements PhoneCatalogue {

	Hashtable h = null;

	public PhoneBook() {
		h = new Hashtable();
	}

	public PhoneAddress getPhoneAddress(String name) {
		PhoneAddress addr = (PhoneAddress)h.get(name);
		return addr;
	}

	private void insert(String name, String number, String address) {
		PhoneAddress addr = new PhoneAddress(name, number, address);
		h.put(name, addr);
	}


	public void readFile(String fileName) throws IOException {
		System.out.println ("Current dir : " + (new File(".")).getCanonicalPath()+"\n");
		BufferedReader in = new BufferedReader(new FileReader(fileName));
		String line = in.readLine();
		while(line != null) {
			StringTokenizer stk = new StringTokenizer(line, "|");
			String name = stk.nextToken();
			String number = stk.nextToken();
			String address = stk.nextToken();
			insert(name, number, address);
			line = null;
			line = in.readLine();
		}
	}
}